package com.umgsai.wx.backend.test;

public class MaxArea {

    /**
     * 容器最大容积
     * https://leetcode-cn.com/problems/container-with-most-water/
     *
     * @param args
     */
    public static void main(String[] args) {
        int[] array = new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7};
        int maxArea = getMaxArea(array);
        System.out.println(maxArea);
    }

    public static int getMaxArea(int[] array) {
        int left = 0;
        int right = array.length - 1;
        int max = Integer.MIN_VALUE;

        while (right > left) {
            int area = getArea(array, left, right);
            max = Math.max(max, area);
            if (array[right] > array[left]) {
                left++;
            } else {
                right--;
            }
        }
        return max;
    }

    public static int getArea(int[] array, int start, int end) {
        return (end - start) * Math.min(array[start], array[end]);
    }
}
